/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.


See LICENSE and AUTHORS for more information.
*/

#include <QtNetwork/QTcpServer>
#include <QtNetwork/QSslSocket>
#include <QtNetwork/QSslCipher>
#include <QtDebug>
#include <QtNetwork/QSslKey>
#include <QtNetwork/QSslCertificate>
#include <QFile>
#include <QSettings>



class CoreXMLParser;
class CoreXMLGenerator;


#ifndef _CORESERVER_H
#define	_CORESERVER_H

class CoreServer : public QTcpServer
{
    Q_OBJECT

    public:
        CoreServer(QString key,QString cert);
        virtual ~CoreServer() {}
        CoreXMLGenerator* getXMLGenerator(QString id);
        CoreXMLParser* getXMLParser(QString id);

        QString getUsername(QString id);
        QString getId(CoreXMLParser* parser);
        QList<QString> getIds(QString username);

    public slots:
        void errorMessage(int errnum, QString errstr);
        void send(QString username, QString data);

    private slots:

        void readyRead();
        void encrypted();
        void sslModeChanged( QSslSocket::SslMode );
        void sslErrors( const QList<QSslError>& );
        void disconnected();
        void connected();
        void error( QAbstractSocket::SocketError );
        void stateChanged( QAbstractSocket::SocketState );


    signals:
        void clientDisposed(QString);
        void authenticationRequest(QString,QString);
        void connectXMLTools(QString);

    protected:
        virtual void incomingConnection( int socketDescriptor );

    private:
        /* id to socket */
        QHash<QString,QSslSocket*> m_sockets;

        /* id to parser */
        QHash<QString, CoreXMLParser*> m_parsers;

        /* id to generator */
        QHash<QString, CoreXMLGenerator*> m_generators;

        /* username to id */
        QHash<QString,QString> m_usernames;

        QString keyFile;
        QString certFile;
};

#endif	/* _CORESERVER_H */

